<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
	<div class="layui-form-item">
	    <label class="layui-form-label">用户id</label>
	    <div class="layui-input-block">
	        <input type="number" name="uid" id="uid"  class="layui-input"  required readonly="true"/>
	    </div>
	</div>
	<div class="layui-form-item">
	    <label class="layui-form-label">产品id</label>
	    <div class="layui-input-block">
	        <input type="number" name="pid" id="pid"   class="layui-input"  required readonly="true"/>
	    </div>
	</div>
	  <div class="layui-form-item">
	      <label class="layui-form-label">产品类别</label>
	      <div class="layui-input-block">
	          <input type="text" name="ptype" id="ptype"  class="layui-input"  required />
	      </div>
	  </div>
	  <div class="layui-form-item">
	      <label class="layui-form-label">产品名称</label>
	      <div class="layui-input-block">
	          <input type="text" name="pname" id="pname"  class="layui-input" lay-verify="required" required>
	      </div>
	  </div>

	  <div class="layui-form-item">
	      <label class="layui-form-label">原产地</label>
	      <div class="layui-input-block">
	          <input type="text" id="production" name="production" class="layui-input" lay-verify="required"  required>
	      </div>
	  </div> 
	  <div class="layui-form-item">
	      <label class="layui-form-label">生产时间</label>
	      <div class="layui-input-block">
	          <input id="picking" name="picking"  class="layui-input" lay-verify="required" required>
	      </div>
	  </div> 
	  <div class="layui-form-item">
	      <label class="layui-form-label">发货时间</label>
	      <div class="layui-input-block">
	          <input id="shipping" name="shipping" class="layui-input" lay-verify="required" required/>
	      </div>
	  </div> 
	  <div class="layui-form-item">
	      <label class="layui-form-label">收获时间</label>
	      <div class="layui-input-block">
	          <input  id="receiving" name="receiving"  class="layui-input" lay-verify="required" required>
	      </div>
	  </div>
	  <div class="layui-form-item">
	      <label class="layui-form-label">目的地</label>
	      <div class="layui-input-block">
	          <input type="text" name="destination" id="destination"  class="layui-input" lay-verify="required" required>
	      </div>
	  </div> 
	  <div class="layui-form-item">
	      <label class="layui-form-label">保质期</label>
	      <div class="layui-input-block">
	          <input type="text" name="validity" id="validity"   class="layui-input" lay-verify="number" required>
	      </div>
	  </div>
	<div class="layui-form-item">
		<label class="layui-form-label">零售价</label>
		<div class="layui-input-block">
			<input type="text" name="price" id="price" class="layui-input" lay-verify="number" required>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">是否召回</label>
		<div class="layui-input-block">
			<input type="number" name="isback" id="isback" class="layui-input" lay-verify="number" required>
		</div>
	</div>
      <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
      </div>	  
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form','laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
			laydate = layui.laydate,
            $ = layui.$;
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            // var index = layer.alert(JSON.stringify(data.field),
            var index = layer.alert("确认提交更改？", 
			function () {
				// 关闭弹出层
				layer.close(index);
				var iframeIndex = parent.layer.getFrameIndex(window.name);
				parent.layer.close(iframeIndex);
				$.ajax({
					url: 'http://localhost:9906/product/update',
					type: 'post',
					data: JSON.stringify(data.field),
					dataType: "json",
					contentType: "application/json",
					success:function(response){
						console.log(response);
						if(response.code==200){
							layer.alert("修改成功！");	
						}else{
							layer.alert("修改失败！");
						}									
					},
					error:function(){
						layer.alert("交互出现错误！请联系管理员！");
					}	
				});	               
            });
            return false;
        });	
							
		//根据url获取传递来的sid					
		var pid= GetQueryString("pid"); // "add"		
		//封装GetQueryString()方法来获取URL后面的value值
		 $("#pid").val(pid);
		 setOldMess(pid);
		//日期时间选择器
		  laydate.render({
		    elem: '#picking'
		    ,type: 'datetime'
		  });
		  laydate.render({
		    elem: '#shipping'
		    ,type: 'datetime'
		  });
		  laydate.render({
		    elem: '#receiving'
		    ,type: 'datetime'
		  }); 	 
		// 获取并初始化修改表单 
		function setOldMess(pid){
			$.ajax({
				url: "http://localhost:9906/product/getproduct/"+pid,
				type:"get",
				success:function(data){
					$("#uid").attr("value",data.data.uid);
					$("#pid").attr("value",data.data.pid);
					$("#pname").attr("value",data.data.pname);
					$("#ptype").attr("value",data.data.ptype);
					$("#production").attr("value",data.data.production);
					$("#destination").attr("value",data.data.destination);
					$("#shipping").attr("value",data.data.shipping);
					$("#receiving").attr("value",data.data.receiving);
					$("#picking").attr("value",data.data.picking);
					$("#validity").attr("value",data.data.validity);
					$("#price").attr("value",data.data.price);
					$("#isback").attr("value",data.data.isback);					
				}
			});	
		}
		
		function GetQueryString(name) {
		     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		     var r = window.location.search.substr(1).match(reg);
		     if (r != null)
		         return decodeURI(r[2]);
		     return null;
		};
    });
</script>
</body>
</html>